<template>
	<view>
		<view class="head-body">
			<uni-section title="第一步: 扫工单码" titleColor="#0055ff" type="line" titleFontSize="20px">
				<uni-row class="demo-uni-row">
					<uni-col :span="5">
						<view class="gd-text">MO号:</view>
					</uni-col>
					<uni-col :span="13">
						<uni-easyinput trim="all" @change="changeMo" v-model="MO" placeholder=""></uni-easyinput>
					</uni-col>
					<uni-col :span="6">
						<yt-scanCode btnName="扫码" style="margin-left: 18%;margin-top: 3px;"
							@getScanCode="OnScanningCode"></yt-scanCode>
					</uni-col>
				</uni-row>
				<uni-row class="demo-uni-row">
					<uni-col :span="24">
						<view class="gl-text">工序号：{{jobCode}}</view>
					</uni-col>
				</uni-row>
				<uni-row class="demo-uni-row">
					<uni-col :span="24">
						<view class="gl-text">工序名称：{{jobName}}</view>
					</uni-col>
				</uni-row>
				<uni-row class="demo-uni-row">
					<uni-col :span="24">
						<view class="gl-text">原库位信息：{{logisticsposition}}</view>
					</uni-col>
				</uni-row>
			</uni-section>
			<uni-section title="第二步: 扫库位码" titleColor="#0055ff" type="line" titleFontSize="20px">
				<uni-row class="demo-uni-row">
					<uni-col :span="5">
						<view class="gd-text">库位码:</view>
					</uni-col>
					<uni-col :span="13">
						<uni-easyinput trim="all" :disabled="true" v-model="WarehouseLocationCode"
							placeholder=""></uni-easyinput>
					</uni-col>
					<uni-col :span="6">
						<yt-scanCode btnName="扫码" style="margin-left: 18%;margin-top: 3px;"
							@getScanCode="OnWarehouse"></yt-scanCode>
					</uni-col>
				</uni-row>
			</uni-section>
		</view>
		<sl-pda-employ :config="pdaConfig"></sl-pda-employ>
	</view>
</template>

<script>
	import {
		getDetailByJobMo,
		save
	} from '@/api/system/warehouse'
	import slPdaEmploy, {
		initPdaScan,
		manualTrigScan,
		onUnloadPdaScan,
		BroadcastConfig
	} from '../../uni_modules/sl-pdaEmploy'
	export default {
		data() {
			return {
				pdaConfig: { // 可以抽离出去管理这个配置
					BroadcastAction: 'nantong',
					BroadcastLabelKey: 'data',
					//BroadcastTriglKey: 'com.scan.onStartScan',
				},
				MO: "",
				WarehouseLocationCode: "",
				jobId: "",
				procId: "",
				jobCode: "",
				jobName: "",
				logisticsposition: "",
				stutStatecode: "",
				jobPrudctNum: "",
			}
		},
		components: {
			slPdaEmploy
		},
		onLoad() {
			initPdaScan(code => {
				if (code === undefined && code === "") {
					uni.showToast({
						title: "无效二维码",
						icon: "error",
						duration: 1500
					})
				} else {
					if (code.substring(0, 2) === "MO") {
						this.clear();
						this.MO = code;
						this.MoRead(code);
					} else {
						if (this.MO === "") {
							uni.showToast({
								title: "请先扫工单码",
								icon: "error",
								duration: 1500
							})
						} else {
							this.WarehouseLocationCode = code;
							this.save();
						}
					}
				}
			})
		},
		onUnload() {
			onUnloadPdaScan()
		},
		methods: {
			save() {
				let data = {
					"jobId": this.jobId,
					"procId": this.procId,
					"jobCode": this.jobCode,
					"jobName": this.jobName,
					"warehouselocationcode": this.WarehouseLocationCode,
					"jobPrudctNum": this.jobPrudctNum,
					"name": this.$store.state.user.name,
					"code": this.$store.state.user.userCode,
					"stutStatecode": this.stutStatecode,
					"time": new Date(),
				}
				console.log(data)
				save(data).then(res => {
					if (res.code === 200) {
						this.logisticsposition = this.WarehouseLocationCode;
						uni.showToast({
							title: "保存成功",
							icon: "success",
							duration: 1500
						})
						setTimeout(() => {
							this.clear();
						}, 2000);
					} else {
						uni.showToast({
							title: "保存失败",
							icon: "error",
							duration: 1500
						})
					}
				})
			},
			OnScanningCode(res) {
				this.clear();
				this.MO = res;
				this.MoRead(res);
			},
			OnWarehouse(res) {
				if (this.MO === "") {
					uni.showToast({
						title: "请先扫工单码",
						icon: "error",
						duration: 1500
					})
				} else {
					this.WarehouseLocationCode = res;
					this.save();
				}

			},
			clear() {
				this.MO = "";
				this.WarehouseLocationCode = "";
				this.jobId = "";
				this.procId = "";
				this.jobCode = "";
				this.jobName = "";
				this.logisticsposition = "";
				this.jobPrudctNum = "";
				this.stutStatecode = "";
			},
			changeMo(e) {
				this.clear();
				if (e.length < 4) {
					uni.showToast({
						title: "工单码错误",
						icon: "error",
						duration: 1500
					})
				} else if (e.substring(0, 2) === "MO") {
					this.MoRead(e);
				} else {
					uni.showToast({
						title: "工单码错误",
						icon: "error",
						duration: 1500
					})
				}
			},
			// 扫工单码
			MoRead(mo) {
				getDetailByJobMo(mo).then(res => {
					if (res.code == 200) {
						let data = res.data;
						this.jobId = data.jobId;
						this.procId = data.procId;
						this.jobCode = data.jobCode;
						this.jobName = data.jobName;
						this.jobPrudctNum = data.jobPrudctNum;
						this.stutStatecode = data.stutStatecode;
						this.logisticsposition = data.logisticsposition === null ? "" : data.logisticsposition;
						console.log(data)
					} else {
						uni.showToast({
							title: "未查询工单",
							icon: "error",
							duration: 1500
						})
					}
				})
			},
		}
	}
</script>

<style>
	.head-top {
		height: 10vh;
	}

	.head-top-row {
		background-color: #007AFF;
		display: flex;
		height: 6vh;
	}

	.head-top-row-left {
		padding-top: 0.5vh;
		font-size: 19px;
		font-weight: 500;
		width: 86%;
	}

	.head-top-row-left-text {
		padding-left: 30%;
		color: #ffffff;
	}

	.head-top-row-right {
		color: #ffffff;
		margin-left: 2%;
		width: 14%;
		font-size: 17px;
		font-weight: 500;
		padding-top: 1vh;
	}

	.head-body {
		background-color: #ffffff;
		height: 99.9vh;
		margin-top: 0.1vh;
	}

	.demo-uni-row {
		padding-left: 10px;
		margin-top: 6px;
	}

	.gd-text {
		font-size: 17px;
		margin-top: 4px;
		float: right;
		margin-right: 6px;
	}

	.gl-text {
		font-size: 17px;
		margin-top: 4px;
		float: left;
		margin-left: 17px;
	}

	::v-deep {
		.uni-card {
			margin: 1% !important;
			padding: 0 !important;

			.uni-card__content {
				padding: 0 !important;
			}
		}
	}

	.getScanCode {
		margin-right: 4px;
		margin-top: 10px;
	}

	::v-deep .is-disabled {
		color: black !important;
	}

	.triangle-facing-left {
		display: inline-block;
		border-left: 1px solid;
		border-bottom: 1px solid;
		width: 12px;
		height: 12px;
		transform: rotate(45deg);
	}
</style>